<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-3-18
 * Time: 下午12:37
 */
namespace Factory\Controller;
use Mcit\Page;
//设备类型
class DeviceController extends BaseController {

    public function index(){
//        P($_SESSION);
        if (session('fac_key')){
            $uid = session("fac_key");
        }else{
            $uid = session("fac_login_name");
        }
        $data = D("Admin/Fac")->getManufacNew($uid);
//        P($data);
        $this->device = $data;
        $this->display();
    }
    //新增一级分类
    public function addCategory(){
        if (session('fac_key')){
            $uid = session("fac_key");
        }else{
            $uid = session("fac_login_name");
        }
        $sqldata['name']            = I("category");
        $sqldata['manufacturer_id'] = M("fac")->where("fac_login_name='".$uid."'")->getField("id");
        $map['name']                = $sqldata['name'];
        $map['manufacturer_id']     = $sqldata['manufacturer_id'];
        if(M("device_type")->where($map)->find()){
            $return['state'] = 200;
            $return['msg']   = "您已添加了该分类";
            echo json_encode($return);
            exit;
        }

        $result = M("device_type")->add($sqldata);
        if($result){
            if(M("device_type")->where("pid='".$result."'")->find() == ''){
                $data['level']           = 2;
                $data['name']            = "未知";
                $data['pid']             = $result;
                $data['manufacturer_id'] = $sqldata['manufacturer_id'];
                M("device_type")->add($data);
            }
            $return['name']  = $sqldata['category'];
            $return['id']    = $result;
            $return['url']   = U('Device/index',array('newid'=>$result));
            $return['state'] = 100;
            $return['msg']   = "添加成功";
        }else{
            $return['state'] = 200;
            $return['msg']   = "添加失败";
        }
        echo json_encode($return);
        exit;
    }
    //新增二级分类
    public function addCategory2(){
        if (session('fac_key')){
            $uid = session("fac_key");
        }else{
            $uid = session("fac_login_name");
        }
        $sqldata['name']            = I("category2");
        $sqldata['pid']             = I("category");
        $sqldata['level']           = 2;
        $sqldata['manufacturer_id'] = M("fac")->where("fac_login_name='".$uid."'")->getField("id");

        $map['name']  = $sqldata['name'];
        $map['manufacturer_id'] = $sqldata['manufacturer_id'];
        if(M("device_type")->where($map)->find()){
            $return['state'] = 200;
            $return['msg']   = "您已添加了该分类";
            echo json_encode($return);
            exit;
        }

        $result = M("device_type")->add($sqldata);
        if($result){
            $return['name']  = $sqldata['name'];
            $return['id']    = $result;
            $return['pid']   = $sqldata['pid'];
            $return['state'] = 100;
            $return['msg']   = "添加成功";
        }else{
            $return['state'] = 200;
            $return['msg']   = "添加失败";
        }
        echo json_encode($return);
        exit;
    }
    //修改一级\二级类型
    public function editCategory(){
        $category = I("category");
        $sqldata['name']  = $category;
        $saveMap['id']    = I("id");
        $result = M("device_type")->where($saveMap)->save($sqldata);
        if($result){
            $return['category'] = $category;
            $return['state'] = 100;
            $return['msg']   = "保存成功";
        }else{
            $return['state'] = 200;
            $return['msg']   = "保存失败";
        }
        echo json_encode($return);
        exit;
    }
    //删除类型
    public function delCategory(){
        $id  = I("id");
        $pid = I("pid");
        if($pid == ""){
            $map['id'] = $id;
        }else{
            $map = "id='".$id."' OR pid='".$id."'";
        }
        $result = M("device_type")->where($map)->delete();
        if($result){
            $return['state'] = 100;
            $return['msg']   = "删除成功";
        }else{
            $return['state'] = 200;
            //$return['msg'] = M("device_type")->getLastSql();
            $return['msg']   = "删除失败";
        }
        echo json_encode($return);
        exit;
    }
}